package com.jdwx.opengl.kotlin2

import java.lang.IllegalArgumentException
import java.util.ArrayList
fun fail(msg:String):Nothing{
    throw IllegalArgumentException(msg)
}
class ThisDemo {
    val thisis = "THIS IS"
    fun whatIsThis(): ThisDemo {
        println(this.thisis) //引用变量
        this.howIsThis()
        // 引用成员函数
        return this
        // 返回此类的引用
    }
    private fun howIsThis(){
        println("HOW IS THIS ?")
    }
}
val <T> List<T>.lastIndex: Int get() = size - 1
fun String.notEmpty(): Boolean {
    return !this.isEmpty()
}
fun MutableList<Int>.swap(index1: Int, index2: Int) {
    val tmp = this[index1]
    // this对应该列表m
    this[index1] = this[index2]
    this[index2] = tmp
}
fun <T> MutableList<T>.mswap(index1: Int, index2: Int) {
    val tmp = this[index1] // “this”对应该列表
    this[index1] = this[index2]
    this[index2] = tmp
}
fun main(){

    //fail("xiaobai")

   // val ex = throw Exception("YYYYYYYY")
    var obj = ThisDemo()
    obj.whatIsThis()

    val sum = fun Int.(x:Int):Int = this * x

    println(sum)

    println( 10.sum( 100 ) )

    val concat = fun String.(x:Any) = this + x


    println( "abc".concat(123) )

    val a = "abc"
   // println(a.notEmpty())

    val mList = mutableListOf<Int>(1, 2, 3, 4, 5)
    println("Before Swap:")
    println(mList)//[1, 2, 3, 4, 5]
    mList.swap(0, mList.size - 1)
    println("After Swap:")
    println(mList)//[5, 2, 3, 4, 1]

    var i = 10
    var j = 1000L

    var z = i + j

    println( "z="+z )


    var aa:Char = '1'

    var bb = aa + i

    println( "bb:$bb" )

    val x = (1 shl 2)

    println( x )

    if( 'a'.compareTo('b')>0  ){
        println("等")
    }else if( 'a'.compareTo('b') <= 0 ){
        println("不等")
    }
    for(c in "abc") {
        println(c)
    }

    println("abc".plus("qqqqqqqqqqqqqqqq"))
    println("abc".subSequence(0,"abc".length))
    var text = """
       ... for(c in "abc")
       ...   print(c)
       ... 
    """
    println("text="+text)
    val square = Array(10, { i -> (i*i)})
    square.forEach {
        println( it )
    }

    fun unitExample(){
        println( "Hello, Unit" )
    }
    var helloUnit = unitExample()

    println( "helloUnit:$helloUnit")

    fun formatCell(value:Double):String=
            if( value.isNaN() ){
                throw IllegalArgumentException("$value is not a number")

            }else{
                value.toString()
            }

    var value = formatCell( 10.0 )

    print("value:$value")

    var list:List<Int> = listOf()

    println( list )

    val list1 = listOf<String>()
    val list2 = listOf(1,2)

    val list3 = listOf(0,1, 2, 3, 4, 5, 6,7,8,9)

    println( list3 )

    val list4 = arrayListOf(0,1,2,3)

    println( list4 )

    val list5 = mutableListOf(1, 2, 3)

    println( list5::class)
    println( list4::class)
    val list6 = listOf(0,1, 2, 3, 4, 5, 6,7,8,9)

    var iterator = list6.iterator()

    println( iterator )

}